package com.stationbuilding.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.stationbuilding.R;
import com.zhy.tree.bean.Node;
import com.zhy.tree.bean.TreeListViewAdapter;
import java.util.List;

public class SelectListAdapter<T> extends TreeListViewAdapter<T> {

    private int selectedPosition = -1;

    public SelectListAdapter(ListView listView, Context context, List<T> list, int defaultExpandLevel, Node node) throws IllegalAccessException {
        super(listView, context, list, defaultExpandLevel, node);
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    @Override
    public View getConvertView(Node node , int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.fragment_tree_select_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.label = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (selectedPosition == position) {
            convertView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.dodgerblue));
        } else {
            convertView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
        }

        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }
        viewHolder.label.setText(node.getName());
        return convertView;
    }

    private final class ViewHolder {
        ImageView icon;
        TextView label;
    }
}
